package org.openfidias.jdbase.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.openfidias.jdbase.db.DbManager;
import org.openfidias.jdbase.locale.Message;
import org.openfidias.jdbase.util.Do;

/**
 *
 * @author atila
 */
public class AlunoDao {

   private static final String table = "aluno.table";
   public static final String logicalTable = "Aluno";
   private static final String deletedTable = "aluno.deleted";

   private AlunoDao() {
   }

   public static AlunoDao getInstance() {
      return AlunoDaoHolder.INSTANCE;
   }

   private static class AlunoDaoHolder {

      private static final AlunoDao INSTANCE = new AlunoDao();
   }

   // TODO: redo create method replacing deleted registers.
   public void create(Aluno aluno) throws IOException, Exception {
      try {
         Do.create(aluno, table, deletedTable);
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   public Aluno read(String login) throws IOException, Exception {
      try {
         Aluno aluno = new Aluno(login);
         return (Aluno) Do.read(aluno, table);
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   public void update(Aluno aluno) {
   }

   public boolean delete(String login) throws IOException, Exception {
      try {
         Aluno aluno = new Aluno(login);
         if (exists(login)) {
            return Do.delete(aluno, table, deletedTable);
         } else {
            // não existe.
            System.out.println(Message.notFound("Login"));
            return false;
         }
      } catch (IOException iOException) {
         throw new IOException(Message.tableNotFound(table));
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   public void list() throws IOException, Exception {
      try {
         File f = DbManager.getTable(table);
         BufferedReader reader = new BufferedReader(new FileReader(f));
         System.out.println(Aluno.headerTable());
         String line = reader.readLine();
         while (line != null) {
            String[] array = line.split(":");
            Aluno aluno = new Aluno(array[1], array[2], Integer.parseInt(array[3]), Short.parseShort(array[0]));
            if (aluno.isNotDeleted()) {
               System.out.println(aluno);
            }
            line = reader.readLine();
         }
         System.out.println(Aluno.rowSeparator());
         reader.close();
      } catch (IOException iOException) {
         throw new IOException(Message.tableNotFound(table));
      } catch (Exception e) {
         throw new Exception(e);
      }
   }

   public boolean exists(String login) throws IOException, Exception {
      Aluno aluno = new Aluno(login);
      return Do.exists(aluno, table);
   }

   /**
    * @deprecated 
    * @return
    * @throws IOException
    */
   public List<Aluno> findAll() throws IOException {
      List<Aluno> list = new ArrayList<Aluno>();
      File f = DbManager.getTable(table);
      BufferedReader reader = new BufferedReader(new FileReader(f));
      String line = reader.readLine();
      while (line != null) {
         String[] array = line.split(":");
         list.add(new Aluno(array[1], array[2], Integer.parseInt(array[3]), Short.parseShort(array[0])));
         line = reader.readLine();
      }
      reader.close();
      return list;
   }
}
